<?php
/********************************************************/
/* Module Name:  userhisto.php						    */
/* Description: displays the user history like login and */
/*    log out time						     			*/
/* Written by: Maria Cielo Carreno							*/
/********************************************************/
session_start();
//If your session isn't valid, it returns you to the login screen for protection
if(empty($_SESSION['myusername'])){
 header("location:main_login.php");
}
//echo $_SESSION['myusername'];
?>

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html>
 <head>
  <link href="default.css" rel="stylesheet" type="text/css" />
 <title>User History</title>
 </head>
 <body>
 <div id="fulladmin">
 <?php
	$uname = $_GET['uname'];
	$id = $_GET['id'];

/* 
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of admin.php that includes pagination
*/
	if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
	{
        // connect to the database
        include('connect-db.php');
        
        // number of results to show per page
        $per_page = 12;
        
        // figure out the total pages in the database
        $result = mysql_query("SELECT * FROM usershisto WHERE UserId=$id ORDER BY datetime DESC");
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];
                
                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }
        
        // display pagination
        echo "<div id='adminleft'>";
        echo "<p>View Page: ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='userhisto.php?page=$i&id=$id&uname=$uname'>$i</a> ";
        }
        echo "</p>";
        echo "</div>";        
        // display data in table
		echo "<div id='adminright'>";
		echo "<center><h1>User History:  $uname </h1><br>";
        echo "<table border='1' cellpadding='10' align='center'>";
        echo "<tr> <th>Activity</th> <th>Date and Time </th> </tr>";

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }
        
                // echo out the contents of each row into a table
                echo "<tr>";
				if(mysql_result($result, $i,'activity') == 1)
					echo "<td> Log In </td>";
				else
					echo "<td> Log Out </td>";
				echo "<td>".mysql_result($result, $i,'datetime')."</td>";
				echo "</tr>";   
        }
        // close table>
        echo "</table>"; 
        
        // pagination
    }    
?>
  </div>
 <div id="footer"><center>Return to main <a href="dashboard.php"><font color="red">Dashboard</font></a>, or you can <a href="log_out.php?id=<?php echo $_SESSION['userid']; ?>"><font color="red">Log Out</font></a></center></div>

 </body>
 </html> 
 